﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace System.Peppers.Wmi
{
    internal static class WmiUtil
    {
        public static DateTime? ToDateTime(object value)
        {
            if (value == null)
            {
                return null;
            }

            try
            {
                return ManagementDateTimeConverter.ToDateTime(Convert.ToString(value));
            }
            catch
            {
                return null;
            }
        }

        public static string ToWmiTime(DateTime? value)
        {
            if (value == null)
            {
                return null;
            }

            try
            {
                return ManagementDateTimeConverter.ToDmtfDateTime(value.Value);
            }
            catch
            {
                return null;
            }
        }

        public static Nullable<T> ToNullable<T>(object value)
            where T : struct
        {
            if (value == null)
            {
                return null;
            }

            if (value is IConvertible)
            {
                return (Nullable<T>)Convert.ChangeType(value, typeof(Nullable<T>));
            }

            return (Nullable<T>)value;
        }
    }
}
